package edu.njit.hz278.tictactoe;

import java.util.HashMap;
import java.util.Map;

import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Toast;

public class WebAppInterface {

	WelcomeActivity ctx;
	WebView wv;

	private Map<String, Object> getMap() {
		return new HashMap<String, Object>();
	}

	WebAppInterface(WelcomeActivity c, WebView w) {
		ctx = c;
		wv = w;
	}

	@JavascriptInterface
	public void signIn(String userid, String passwd) {
		Map<String, Object> param = getMap();
		param.put("action", Constants.ACTION_SIGN_IN);
		param.put("userid", userid);
		param.put("passwd", passwd);

		ctx.addTask(param);
	}
	
	@JavascriptInterface
	public void signUp(String userid, String passwd) {
		Map<String, Object> param = getMap();
		param.put("action", Constants.ACTION_NEW_USER);
		param.put("userid", userid);
		param.put("passwd", passwd);

		ctx.addTask(param);
	}
	
	@JavascriptInterface
	public void move(String row, String col) {
		Map<String, Object> param = getMap();
		param.put("action", Constants.ACTION_MOVE);
		param.put("row", row);
		param.put("col", col);

		ctx.addTask(param);
	}
	
	@JavascriptInterface
	public void createGame(String roomname, String roompasswd) {
		Map<String, Object> param = getMap();
		param.put("action", Constants.ACTION_CREAT_ROOM);
		param.put("name", roomname);
		param.put("passwd", roompasswd);
		ctx.addTask(param);
	}
	
	@JavascriptInterface
	public void joinGame(String roomid, String aid, String ownerid, String roompasswd) {
		Map<String, Object> param = getMap();
		param.put("roomid", Integer.parseInt(roomid));
		param.put("aid", Integer.parseInt(aid));
		param.put("roompasswd", roompasswd);
		param.put("ownerid", ownerid);
		param.put("action", Constants.ACTION_JOIN);
		ctx.addTask(param);
	}
	
	@JavascriptInterface
	public void listPlayers() {
		Map<String, Object> param = getMap();
		param.put("action", Constants.ACTION_LEADER_BOARD);
		ctx.addTask(param);
	}
	
	@JavascriptInterface
	public void listRooms() {
		Map<String, Object> param = getMap();
		param.put("action", Constants.ACTION_LS_ROOM);
		ctx.addTask(param);
	}

	@JavascriptInterface
	public void showToast(String toast) {
		Log.i("from javascript", toast);
		Toast.makeText(ctx, toast, Toast.LENGTH_SHORT).show();
	}

}
